﻿
using System;

namespace Coticula.DTO.Api
{
    public class CoticulaApi
    {
        private readonly JsonWebClient _webClient = new JsonWebClient(@"http://localhost:52974/");

        [Obsolete("Don't use constructor without parameters, use constructor with parameters.")]
        public CoticulaApi()
        {
            
        }

        public CoticulaApi(string url)
        {
            _webClient = new JsonWebClient(url);
        }

        public Login Login { get; set; }

        private string GetWithSignIn(string uri)
        {
            string response = null;
            try
            {
                response = _webClient.Get(uri);
            }
            catch (CoticulaApiAuthorizeException)
            {
                if (Login != null)
                {
                    if (Signin(Login))
                        response = _webClient.Get(uri);
                    else
                        throw;
                }
                else
                {
                    throw;
                }
            }
            return response;
        }

        private string PostWithSignIn(string uri, string json)
        {
            string response = null;
            try
            {
                response = _webClient.Post(uri, json);
            }
            catch (CoticulaApiAuthorizeException)
            {
                if (Login != null)
                {
                    if (Signin(Login))
                        response = _webClient.Post(uri, json);
                    else
                        throw;
                }
                else
                {
                    throw;
                }
            }
            return response;
        }

        public int[] Untested()
        {
            string response = GetWithSignIn(string.Format("/Api/Untested.json"));
            var untested = Serializer.Deserialize<int[]>(response);
            return untested;
        }

        public Solution Solution(int id)
        {
            string response = GetWithSignIn(string.Format("/Api/Solution/{0}.json", id));
            var solution = Serializer.Deserialize<Solution>(response);
            return solution;
        }

        public bool Result(Result result)
        {
            var json = Serializer.Serialize(result);
            PostWithSignIn("/Api/Result.json", json);
            //TODO: 
            return true;
        }

        public bool Signin(Login login)
        {
            var json = Serializer.Serialize(login);
            var respone = _webClient.Post("/Api/Signin.json", json);
            //var respone = _webClient.Get("/Api/Signin.json");
            respone = Serializer.Deserialize<string>(respone);
            return respone == "ok";
        }

        public bool ResultByTests(ResultByTests resultByTests)
        {
            var json = Serializer.Serialize(resultByTests);
            PostWithSignIn("/Api/ResultByTest.json", json);
            //TODO:
            return true;
        }
    }
}
